﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Microsoft.Practices.Composite.Events;

namespace Centrica.Task.App
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static IEventAggregator EventAggregator = new EventAggregator();

        protected override void OnStartup(StartupEventArgs e)
        {
            /*
            base.OnStartup(e);
            Bootstrapper boot = new Bootstrapper();
            boot.Run();
            */

            App.EventAggregator
                .GetEvent<View.TaskNotificationEvent>()
                .Subscribe(OnTaskSelect, true);
            App.EventAggregator
                .GetEvent<View.TaskNewEvent>()
                .Subscribe(OnTaskAddNew, true);
            App.EventAggregator
                .GetEvent<View.TaskUpdateEvent>()
                .Subscribe(OnTaskUpdate, true);
            App.EventAggregator
                .GetEvent<View.TaskRemoveEvent>()
                .Subscribe(OnTaskRemove, true);
        }

        private void OnTaskSelect(Centrica.Task.Model.Task taskObj)
        {
        }

        private void OnTaskUpdate(Centrica.Task.Model.Task taskObj)
        {
            Model.TaskModelFactoryProvider.StaticInstance.Provider
                .UpdateTask(taskObj);
            App.EventAggregator
                .GetEvent<View.TaskListEvent>()
                .Publish(Model.TaskModelFactoryProvider.StaticInstance.Provider);
        }

        private void OnTaskRemove(Centrica.Task.Model.Task taskObj)
        {
            Model.TaskModelFactoryProvider.StaticInstance.Provider
                .RemoveTask(taskObj);
            App.EventAggregator
                .GetEvent<View.TaskListEvent>()
                .Publish(Model.TaskModelFactoryProvider.StaticInstance.Provider);
        }

        private void OnTaskAddNew(Centrica.Task.Model.Task taskObj)
        {
            Model.TaskModelFactoryProvider.StaticInstance.Provider
                .AddNewTask(taskObj);
            App.EventAggregator
                .GetEvent<View.TaskListEvent>()
                .Publish(Model.TaskModelFactoryProvider.StaticInstance.Provider);
        }
    }
}
